<template>
    <div class="order-report">
        <!-- <div class="report-table-top-title">POS机管理</div> -->
        <div class="report-table-form1">
            <div class="report-table-form1-line">
                <div
                    class="report-table-form1-item word samll"
                    style="width: 70px"
                >
                    月份：
                </div>
                <div class="report-table-form1-item" style="width: 290px">
                    <el-date-picker
                        size="small"
                        v-model="startMonth"
                        type="month"
                        value-format="yyyy-MM"
                        placeholder="选择日期"
                        style="width: 130px"
                    ></el-date-picker>
                    <div
                        class="report-table-form1-item word samll"
                        style="width: 12px"
                    >
                        -
                    </div>
                    <el-date-picker
                        size="small"
                        v-model="endMonth"
                        type="month"
                        value-format="yyyy-MM"
                        placeholder="选择日期"
                        style="width: 130px"
                    ></el-date-picker>
                </div>
                <div
                    class="report-table-form1-item word samll"
                    style="width: 80px"
                >
                    代理商：
                </div>
                <div class="report-table-form1-item" style="width: 100px">
                    <el-select
                        v-model="agentName"
                        filterable
                        clearable
                        placeholder="请选择"
                        size="small"
                    >
                        <el-option label="全部" value=""></el-option>
                        <el-option
                            v-for="item in agentList"
                            :key="item.id"
                            :label="item.agentName"
                            :value="item.agentName"
                        ></el-option>
                    </el-select>
                </div>
                <div
                    class="report-table-form1-item word samll"
                    style="width: 80px"
                >
                    借机人：
                </div>
                <div class="report-table-form1-item" style="width: 100px">
                    <el-select
                        v-model="adminCode"
                        clearable
                        filterable
                        placeholder="请选择"
                        size="small"
                    >
                        <el-option label="全部" value=""></el-option>
                        <el-option
                            v-for="item in borrowAdminList"
                            :key="item.id"
                            :label="item.borrowAdminName"
                            :value="item.adminCode"
                        ></el-option>
                    </el-select>
                </div>
                <div
                    class="report-table-form1-item word samll"
                    style="width: 80px"
                >
                    借机编码：
                </div>
                <div class="report-table-form1-item" style="width: 120px">
                    <el-input size="small" v-model="posBorrowCode"></el-input>
                </div>
                <el-button type="primary" size="small" @click="search"
                    >查询</el-button
                >
            </div>
        </div>
        <!-- 第一张表 -->
        <div class="page-padding-gray-small"></div>
        <div class="report-table-wrapper">
            <el-table
                :header-cell-style="headerCellStyle"
                size="small"
                :data="showList"
                border
                :row-class-name="tableRowClassName"
            >
                <!-- 借机编码，借机申请人，借机时间，借机用途，借机数量，发货数量，已核销数量，未核销数量 -->
                <el-table-column
                    prop="agentName"
                    label="代理商"
                ></el-table-column>
                <el-table-column prop="code" label="借机编码"></el-table-column>
                <el-table-column
                    prop="adminName"
                    label="借机申请人"
                ></el-table-column>
                <el-table-column prop="borrowDateStr" width="150" label="借机时间">
                </el-table-column>
                <el-table-column
                    prop="borrowNum"
                    label="借机数量"
                ></el-table-column>
                <el-table-column
                    prop="shipNum"
                    label="发货数量"
                ></el-table-column>
                <el-table-column
                    prop="writeOfNum"
                    label="已核销数量"
                ></el-table-column>
                <el-table-column
                    prop="unWriteOfNum"
                    label="未核销数量"
                ></el-table-column>
                <el-table-column
                    prop="useTo"
                    label="借机用途"
                ></el-table-column>
                <el-table-column width="200" label="操作">
                    <template slot-scope="scope">
                        <el-button
                            type="primary"
                            size="small"
                            @click="toRowDetail(scope.row)"
                            >详情</el-button
                        >
                        <el-button
                            type="primary"
                            size="small"
                            @click="toWriteDetail(scope.row)"
                            >核销信息</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
        </div>
           <div class="pagination">
            <el-pagination
                @current-change="handleCurrentChange"
                :current-page="currentPage"
                :page-size="pageSize"
                layout="prev, pager, next, total"
                :total="pageTotal"
            ></el-pagination>
        </div>
        <!-- 第二张表 -->
        <div class="page-padding-gray-small"></div>
        <div class="report-table-wrapper">
            <el-table
                :header-cell-style="headerCellStyle"
                size="small"
                :data="showList2"
                border
                style="width: 100%"
                :row-class-name="tableRowClassName"
            >
                <!-- 总台数， 已分配台数，未分配台数  操作（进详情页） -->

                <el-table-column
                    prop="adminName"
                    label="借机人名称"
                ></el-table-column>
                <el-table-column
                    prop="totalBorrowNum"
                    label="总台数"
                ></el-table-column>
                <el-table-column
                    prop="totalWriteOfNum"
                    label="已核销台数"
                ></el-table-column>
                <el-table-column
                    prop="totalUnWriteOfNum"
                    label="未核销台数"
                ></el-table-column>
                <!-- <el-table-column prop="firstUseTime" label="操作">
                    <template slot-scope="props">
                        <el-button
                            type="primary"
                            size="small"
                            @click="openAgentDetailDialog(props.row)"
                            >详情</el-button
                        >
                    </template>
                </el-table-column> -->
            </el-table>
        </div>
        <posRowDetail :value.sync="showValue" :currRow="currRow"></posRowDetail>
        <writeInformation :value.sync="showWriteValue" :currRow="currRow"></writeInformation>
    </div>
</template>

<script type="text/ecmascript-6">
import writeInformation from './components/writeInformation'
import posRowDetail from './components/posRowDetail'
export default {
    components: {
        posRowDetail,
        writeInformation
    },
    data: function () {
        return {
            posBorrowCode: '',
            adminCode: '',
            showValue: false,
            showWriteValue: false,
            startMonth: '',
            endMonth: '',
            startMonth2: '',
            endMonth2: '',
            timeStartMonth: '',
            currRow: {},
            companyCode: '',
            agentName: '',
            agentList: [],
            showList: [],
            showList2: [],
            borrowAdminList: [],
            isPass: '',
            isPassList: [
                {
                    isPassName: '是',
                    isPass: true
                }, {
                    isPassName: '否',
                    isPass: false
                }
            ],
            showTotalList: [
            ],
            currentPage: 1,
            pageSize: 10,
            pageTotal: 0,
            showJiejiOperation: false,
            jiejiOperationList: [
                {
                    agent: '代理商1',
                    num: 1
                }
            ],
            showAgentDetailDialog: false,
            agentDetailList: [
                {
                    agent: '代理商1',
                    num: 1
                }
            ],
            ip: ''
        }
    },
    created () {
        this.getAllAgent()
        this.getBorrowAdminList()
        this.startMonth = this.$dateToStringYM(new Date())
        this.endMonth = this.$dateToStringYM(new Date())
        this.search()
    },
    computed: {
        isCompany () {
            return this.$store.state.isCompany
        },
        companyList () {
            return this.$store.state.companyList.slice(1)
        },
        roleId () {
            return this.$store.state.roleId
        }
    },
    methods: {
        toRowDetail (row) {
            console.log(row)
            this.currRow = row
            this.showValue = true
        },
        toWriteDetail (row) {
            console.log(row)
            this.currRow = row
            this.showWriteValue = true
        },
        async getBorrowAdminList () {
            let res = await this.$http.get(this.$store.state.IP + '/borrowDevice/getBorrowAdminList', {
                data: {}
            })
            let response = res.data
            if (response.ret === '0') {
                console.log(res)
                this.borrowAdminList = response.res
            }
        },
        async getAllAgent () {
            let res = await this.$http.get(this.$store.state.IP + '/systemUserManage/getAgents', {
                data: {}
            })
            let response = res.data
            if (response.ret === '0') {
                console.log(res)
                this.agentList = response.res
            }
        },
        tableRowClassName ({ row, rowIndex }) {
            // 表格行 标红
            // if (!row.isPass) {
            //     return 'error-row'
            // }
            return ''
        },
        search () {
            this.currentPage = 1
            // if (!this.startMonth || !this.endMonth) {
            //     this.$message({
            //         message: '请先选择时间！',
            //         type: 'warning'
            //     })
            //     return false
            // }
            if (this.startMonth > this.endMonth) {
                this.$message({
                    message: '开始时间不能大于结束时间！',
                    type: 'warning'
                })
                return false
            }
            this.borrowPosAdminStatistics()
            this.getReport()
        },
        borrowPosAdminStatistics () {
            // pos管理流水查询
            let data = {
                adminCode: this.adminCode,
                agentName: this.agentName,
                beginDate: this.startMonth,
                posBorrowCode: this.posBorrowCode,
                endDate: this.endMonth,
                type: 1
            }
            this.$http.post(this.$store.state.IP + '/borrowDevice/borrowPosAdminStatistics', data,
                {
                    'emulateJSON': false
                }
            ).then(response => {
                console.log('-------------------------------------------')
                response = response.body
                console.log(response)
                if (response.ret === '0') {
                    this.showList2 = response.res || []
                }
            }, response => {
                console.log('通讯失败')
            })
        },
        getReport () {
            // pos管理流水查询
            let data = {
                adminCode: this.adminCode,
                agentName: this.agentName,
                beginDate: this.startMonth,
                endDate: this.endMonth,
                posBorrowCode: this.posBorrowCode,
                page: this.currentPage - 1,
                limit: this.pageSize
            }
            this.$http.post(this.$store.state.IP + '/borrowDevice/borrowPosList', data,
                {
                    'emulateJSON': false
                }
            ).then(response => {
                console.log('-------------------------------------------')
                response = response.body
                console.log(response)
                if (response.ret === '0') {
                    this.showList = response.res.list || []
                    this.pageTotal = response.res.total
                }
            }, response => {
                console.log('通讯失败')
            })
        },
        handleCurrentChange (value) {
            // 换页
            this.currentPage = value
            this.getReport()
        },
        closeJiejiDialog () {
            // 关闭借机弹窗
            this.showJiejiOperation = false
        },
        openAgentDetailDialog () {
            // 打开 代理商详情弹窗
            this.showAgentDetailDialog = true
        },
        closeAgentDetailDialog () {
            // 打开 代理商详情弹窗
            this.showAgentDetailDialog = false
        }
    }
}
</script>

<style lang="stylus" rel="stylesheet/stylus">
.el-table
    .error-row
        background: #f5dede

.order-report
    width: 100%
    background: #fff
    min-height: calc(100vh - 65px)

    .report-table-top-title
        width: 100%
        height: 64px
        line-height: 64px
        color: #000
        font-size: 18px
        background: #f1f0f5

    .report-table-count
        margin: 30px 0px 10px 10px

    .report-table-form1
        width: 100%
        min-height: 40px
        padding: 16px 0 0 22px
        box-sizing: border-box
        background: #fff
        overflow: hidden

        .report-table-form1-line
            min-height: 32px
            margin-bottom: 16px

            .report-table-form1-item
                display: inline-block
                vertical-align: top
                height: 32px
                margin-right: 15px
                line-height: 32px

                &.word
                    margin-right: 0
                    text-align right
                &.samll
                    font-size: 15px
                    text-align: right

                &.big
                    margin-right: 80px

    .report-table-block
        width: 100%
        padding-top: 22px
        height: 50px
        background: #fff
        box-sizing: border-box

        .report-table-block-in
            float: right
            padding-right: 17px

    .page-padding-gray-small
        width: 100%
        height: 22px
        background: #f1f0f5

    .page-padding
        width: 100%
        height: 20px
        background: #fff

    .serviceImg
        height: 150px

    .report-table-total
        width: 100%
        font-size: 16px
        color: #409EFF
        padding: 22px
        margin-top: 22px
        box-sizing: border-box
        background: #fff

.col-red
    color: red

.pagination
    padding: 20px 0
    text-align: center

.agent-dialog
    min-height: 200px
    max-height: 70vh
    overflow-y: scroll

.agent-dialog-footer
    text-align: center
    margin-top: 50px
</style>
